(function(app){
	app
		.controller("userLoginCtrl",["$scope","myFactory",'$http','$state','$stateParams','$location','$ionicHistory',function($scope,myFactory,$http,$state,$stateParams,$location,$ionicHistory){

			$http({
				method: 'POST',
				url: app.domain + 'user/restUserLogin.action'
				//params: {'indexUrl': indexUrl,'userInfoLocalStr': userInfoLocalStr}
			}).then(function successCallback(response) {
				if (response.data.success) {
					//得到用户信息，保存到缓存中，在本登录中会话失效后再次登录的时候默认该用户
					/*var userInfo = response.data.data.user
					if(""!=userInfo && null!=userInfo && undefined!=userInfo){
						//localStorage.setItem("userInfo", userInfo);
						var userInfo = JSON.stringify(userInfo);
						//存入缓存sessionStorage
						//sessionStorage.obj = userInfo;
						sessionStorage.setItem("userInfo", userInfo);
						//存入缓存localStorage
						//localStorage.setItem("userInfo", userInfo);
						console.log("userInfo:"+userInfo);
					}*/
					var token = response.data.data.token;
					if(""!=token && null!=token && undefined!=token){
						var userInfo = JSON.stringify(userInfo);
						//存入缓存sessionStorage
						sessionStorage.setItem("token", token);
						console.log("token:"+token);
					}
					//成功获取用户信息跳转查询页面
					$ionicHistory.clearCache().then(function(){ 
						$state.go('search',{
							login:'yes'
						});
					});
				}else{//属于未登录情况的交由app拦截器处理
					$.hideLoading();
					var message = response.data.msg;//返回信息
					/*$.alert({
						title: '',
						text: message,
						onOK: function () {
							//点击确认
							var data = {
									sessionTimeOut : "No", //"Yes" Session超时 "No":系统正常的返回
									exitSysPage : "1" //"1" 信易通首页  "2":“我”页面
							};
							cordova.sino.entryLocalSys(function(){}, data);
						}
					});*/
					$ionicHistory.clearCache().then(function(){ 
						$state.go('message',{
							fromPage:"1",
							message:message
						});
					});
				}
				//$location.path('search');
				//window.location.replace("index.html#/search");//成功验证后，强制改变地址栏参数，跳转查询页面
			}, function errorCallback(response) {
				$.hideLoading();
				$.confirm("用户登录失败,是否退出系统？", "", function() {
					console.log("用户登录失败,退出系统！");
					//直接退出到XYT2.0
					var data = {
						sessionTimeOut : "No", //"Yes" Session超时 "No":系统正常的返回
						exitSysPage : "1" //"1" 信易通首页  "2":“我”页面	
					};
					setTimeout(function() {
						//cordova.sino.entryLocalSysFromOtherSys(function(){}, data);
						cordova.sino.entryLocalSys(function(){}, data);
					}, 350);
				});
			 });

		}]);
})(app)